#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHeaderView>
#include "qxtglobalshortcut.h"
#include <QDebug>
#include "keysequencedelegate.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_hotkeyModel(new HotKeyTableModel(this))
{
    ui->setupUi(this);
    initUi();

 //   connect(this, SIGNAL(sig_dataChanged ( int , const int , const QString& )), m_hotkeyModel, SIGNAL(sig_dataChanged ( int , const int , const QString& )));
    //  connect(m_hotkeyModel, SIGNAL(sig_dataChanged ( int , const int , const QString& )), this, SLOT(sig_dataChanged ( int , const int , const QString& )));

    QxtGlobalShortcut *shortcut = new QxtGlobalShortcut(this);
    if(shortcut->setShortcut(QKeySequence("F1")))
    {
        connect(shortcut, &QxtGlobalShortcut::activated,
                [=]() {
                    qDebug() << "shortcut activated";
        });
    }
    else
    {
        qDebug()<<"快捷键已占用";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUi() {
    if(m_hotkeyModel == nullptr) return ;
   // ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStringList titles = {tr("功能说明"), tr("键值说明"), tr("说明")};
    m_hotkeyModel->setTableHeaderData(titles);

    ui->tableView->setShowGrid(true); //设置显示格子线
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);//设置选中单个
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);//双击编辑

    //ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑

   ui->tableView->horizontalHeader()->setStretchLastSection(true); //行头自适应表格

    //点击表时不对表头行光亮（获取焦点）
    ui->tableView->horizontalHeader()->setHighlightSections(false);

    // 设置列宽不可变动，即不能通过鼠标拖动增加列宽
//    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    //设置表格的各列的宽度值
    //设置单元格大小
   // ui->tableView->horizontalHeader()->setDefaultSectionSize(50);  //设置默认宽度
   // ui->tableView->verticalHeader()->setDefaultSectionSize(24);   //设置一行默认高度

    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自动设置行高
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自动设置列宽
    //下面是通过表头设置列宽
   ui->tableView->horizontalHeader()->setMinimumSectionSize(120);//设置最小列宽
//    ui->tableView->horizontalHeader()->setMaximumSectionSize(300);//设置最大列宽
    //ui->tableView->setColumnWidth(0, 300);//设置第1列宽100
    //设置第3列自动...
    //ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
   // ui->tableView->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch);

    ui->tableView->setModel(m_hotkeyModel);

    ui->tableView->setAlternatingRowColors(true);

    KeySequenceDelegate* delegateSensorTypes = new KeySequenceDelegate( this);
    ui->tableView->setItemDelegateForColumn(1, delegateSensorTypes);

    initData();
}

void MainWindow::initData(void) {
    QMap<int, QVector<QString>> mapData;
    QVector<QString> vec;
    vec.push_back(tr("标准下单"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(0, vec);
    vec.clear();
    vec.push_back(tr("沽出"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(1, vec);
    vec.clear();
    vec.push_back(tr("买入"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(2, vec);
    vec.clear();
    vec.push_back(tr("价格输入框值加（+）    "));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(3, vec);
    vec.clear();
    vec.push_back(tr("价格减"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(4, vec);
    vec.clear();
    vec.push_back(tr("数量加"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(5, vec);
    vec.clear();
    vec.push_back(tr("数量减"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(6, vec);
    vec.clear();
    vec.push_back(tr("重置"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(7, vec);
    vec.clear();
    vec.push_back(tr("确定"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(8, vec);
    vec.clear();
    vec.push_back(tr("简洁行情"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(9, vec);
    vec.clear();
    vec.push_back(tr("委托查询"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(10, vec);
    vec.clear();
    vec.push_back(tr("订单复核"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(11, vec);
    vec.clear();
    vec.push_back(tr("客户查询"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(12, vec);
    vec.clear();
    vec.push_back(tr("批量撤单"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(13, vec);
    vec.clear();
    vec.push_back(tr("报表导出"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(14, vec);
    vec.clear();
    vec.push_back(tr("切换交易日"));
    vec.push_back(tr("无"));
    vec.push_back(tr("未设置"));
    mapData.insert(15, vec);
    vec.clear();
    this->m_hotkeyModel->setSourceData(mapData);


}

void MainWindow::changeEvent(QEvent *event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        translateUI();
        break;
    default:
        QWidget::changeEvent(event);
    }
}

void MainWindow::translateUI()
{
    this->setWindowTitle(tr("Set Hotkey "));
}

void MainWindow::switchLanguage(LANGUAGE language)
{
    QString language_qm;
    switch(language)
    {
    case UI_ZH:
        language = UI_ZH;
        language_qm = QString(":/qm/language_zh");
        break;

    case UI_EN:
        language = UI_EN;
        language_qm = QString(":/qm/language_en");
        break;

    case UI_TC:
        language = UI_TC;
        language_qm = QString(":/qm/language_tc");
        break;
    default:
        language = UI_ZH;
        language_qm = QString(":/qm/language_tc");
    }

    if(m_curLanguage != language)
    {
        m_curLanguage = language;
        m_pTranslator->load(language_qm);
       // Util::writeInit(QString("./user.ini"), QString("language"), QString::number(language, 10));
    }
}







